package com.buddy.learn.algorithm.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author Buddy
 * @date 2021/5/6
 */
public class _020密码合格校验 {

    private static Pattern p1 = Pattern.compile("[A-Z]");
    private static Pattern p2 = Pattern.compile("[a-z]");
    private static Pattern p3 = Pattern.compile("[0-9]");
    private static Pattern p4 = Pattern.compile("[^a-zA-Z0-9]");

    /**
     * 密码要求:
     * 1.长度超过8位
     * 2.包括大小写字母.数字.其它符号,以上四种至少三种
     * 3.不能有相同长度大于2的子串重复
     */
    public static void main(String[] arg) {
        List<String> list = new ArrayList<>();
        list.add("021Abc9000");
        list.add("021Abc9Abc1");
        list.add("021ABC9000");
        list.add("021$bc9000");
        for (String str : list) {
            if (str.length() <= 8) {
                System.out.println("NG");
                continue;
            }

            if (getMatch(str)) {
                System.out.println("NG");
                continue;
            }

            if (getString(str, 0, 3)) {
                System.out.println("NG");
                continue;
            }
            System.out.println("OK");
        }
    }

    private static boolean getMatch(String str) {
        int count = 0;

        if (p1.matcher(str).find()) {
            count++;
        }
        if (p2.matcher(str).find()) {
            count++;
        }
        if (p3.matcher(str).find()) {
            count++;
        }
        if (p4.matcher(str).find()) {
            count++;
        }
        if (count >= 3) {
            return false;
        } else {
            return true;
        }
    }

    private static boolean getString(String str, int left, int right) {
        if (right >= str.length()) {
            return false;
        }
        if (str.substring(right).contains(str.substring(left, right))) {
            return true;
        } else {
            return getString(str, left + 1, right + 1);
        }
    }
}
